package actions;
import java.sql.Timestamp;
import java.text.*;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
import beans.AbstractPagedQuery;
import service.DeliveryOrderService;
import beans.DeliveryOrderQuery;
import domain.DeliveryOrder;
/**
 * @author system auto
 */
public class DeliveryOrderMainAction extends AbstractPagedQueryAction {
	/**
	 * 用于查询订单.
	 */
	 private DeliveryOrderService deliveryOrderService;
	/**
	  * 构造分页查询对象并进行分页查询.
	  * @param form action form
	  * @param request http request
	  * @param totalCount 记录总个数
	  * @param pagerOffset 查询的起始纪录位置
	  * @param itemsPerPage 每页多少条纪录
	  * @return AbstractPagedQuery Query
	  * @throws Exception any exception
	  */
	 @Override
	 public AbstractPagedQuery<DeliveryOrder> doPagedQuery(ActionForm form,
	       HttpServletRequest request, int totalCount, int pagerOffset,
	       int itemsPerPage) throws Exception {
	   DynaValidatorForm queryCondForm = (DynaValidatorForm) form;
	   DeliveryOrderQuery myQuery = new DeliveryOrderQuery();
	   // 设置查询的分页参数
	   myQuery.setItemsPerPage(itemsPerPage);
	   myQuery.setTotalCount(totalCount);
	   myQuery.setPagerOffset(pagerOffset);
	   	String orderNumber = queryCondForm.getString("qOrderNumber");
		if (null != orderNumber && !"".equals(orderNumber)) {
	   		myQuery.setOrderNumber(orderNumber);
	   	}
	   	String orderType = queryCondForm.getString("qOrderType");
		if (null != orderType && !"".equals(orderType)) {
	   		myQuery.setOrderType(orderType);
	   	}
	   	String vipCustomFlag = queryCondForm.getString("qVipCustomFlag");
		if (null != vipCustomFlag && !"".equals(vipCustomFlag)) {
	   		myQuery.setVipCustomFlag(vipCustomFlag);
	   	}
	   	String sender = queryCondForm.getString("qSender");
		if (null != sender && !"".equals(sender)) {
	   		myQuery.setSender(sender);
	   	}
	   	String receiver = queryCondForm.getString("qReceiver");
		if (null != receiver && !"".equals(receiver)) {
	   		myQuery.setReceiver(receiver);
	   	}
	   	String originLocation = queryCondForm.getString("qOriginLocation");
		if (null != originLocation && !"".equals(originLocation)) {
	   		myQuery.setOriginLocation(originLocation);
	   	}
	   	String destLocation = queryCondForm.getString("qDestLocation");
		if (null != destLocation && !"".equals(destLocation)) {
	   		myQuery.setDestLocation(destLocation);
	   	}
	   	String payType = queryCondForm.getString("qPayType");
		if (null != payType && !"".equals(payType)) {
	   		myQuery.setPayType(payType);
	   	}
	   	String orderDate = queryCondForm.getString("qOrderDate");
		if (null != orderDate && !"".equals(orderDate)) {
	   		DateFormat DATE_FAMAT_ORDERDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
	   		myQuery.setOrderDate(new Timestamp(DATE_FAMAT_ORDERDATE.parse(orderDate).getTime()));
	   	}
	   	String goodsName = queryCondForm.getString("qGoodsName");
		if (null != goodsName && !"".equals(goodsName)) {
	   		myQuery.setGoodsName(goodsName);
	   	}
	   	String serviceType = queryCondForm.getString("qServiceType");
		if (null != serviceType && !"".equals(serviceType)) {
	   		myQuery.setServiceType(serviceType);
	   	}
	   	String subStation = queryCondForm.getString("qSubStation");
		if (null != subStation && !"".equals(subStation)) {
	   		myQuery.setSubStation(subStation);
	   	}
	   	String creater = queryCondForm.getString("qCreater");
		if (null != creater && !"".equals(creater)) {
	   		myQuery.setCreater(creater);
	   	}
	   	String fetcher = queryCondForm.getString("qFetcher");
		if (null != fetcher && !"".equals(fetcher)) {
	   		myQuery.setFetcher(fetcher);
	   	}
	   	String dispatcher = queryCondForm.getString("qDispatcher");
		if (null != dispatcher && !"".equals(dispatcher)) {
	   		myQuery.setDispatcher(dispatcher);
	   	}
	   	String orderStatus = queryCondForm.getString("qOrderStatus");
		if (null != orderStatus && !"".equals(orderStatus)) {
	   		myQuery.setOrderStatus(orderStatus);
	   	}
	   	String scheduler = queryCondForm.getString("qScheduler");
		if (null != scheduler && !"".equals(scheduler)) {
	   		myQuery.setScheduler(scheduler);
	   	}
   		// 调用DeliveryOrderService进行分页查询
        deliveryOrderService.queryDeliveryOrders(myQuery);
        return myQuery;
  	}
  /**
   * 批量删除订单.
   * @param mapping mapping
   * @param form v
   * @param request
   * @param response
   * @return action forward
   * @throws Exception
   */
  public ActionForward batchDelete(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {
      String[] ids = request.getParameterValues("ids");
      if (ids != null && !(ids.length == 0)) {
          deliveryOrderService.delDeliveryOrderByIDs(ids);
      }
      return mapping.findForward("main");
  }
  /**
    * 显示新增界面.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayDeliveryOrderCreateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       // 产生并保存Token,防止重复提交
       saveToken(request);
       // 初始化输入框
       DynaValidatorForm deliveryOrderForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("deliveryOrderForm")
               .createActionForm(servlet); 
        deliveryOrderForm.set("id", ""); 
        deliveryOrderForm.set("orderNumber", ""); 
        deliveryOrderForm.set("orderType", ""); 
        deliveryOrderForm.set("vipCustomFlag", ""); 
        deliveryOrderForm.set("sender", ""); 
        deliveryOrderForm.set("fetchAddr", ""); 
        deliveryOrderForm.set("senderPhone", ""); 
        deliveryOrderForm.set("receiver", ""); 
        deliveryOrderForm.set("receiverAddr", ""); 
        deliveryOrderForm.set("receiverPhone", ""); 
        deliveryOrderForm.set("originLocation", ""); 
        deliveryOrderForm.set("destLocation", ""); 
        deliveryOrderForm.set("chargeWeight", ""); 
        deliveryOrderForm.set("dimension", ""); 
        deliveryOrderForm.set("contentsDesc", ""); 
        deliveryOrderForm.set("payType", ""); 
        deliveryOrderForm.set("specialOperateNote", ""); 
        deliveryOrderForm.set("insuranceFee", ""); 
        deliveryOrderForm.set("agencyReceiveFee", ""); 
        deliveryOrderForm.set("orderDate", ""); 
        deliveryOrderForm.set("goodsName", ""); 
        deliveryOrderForm.set("goodsPrice", ""); 
        deliveryOrderForm.set("serviceType", ""); 
        deliveryOrderForm.set("subStation", ""); 
        deliveryOrderForm.set("creater", ""); 
        deliveryOrderForm.set("fetcher", ""); 
        deliveryOrderForm.set("dispatcher", ""); 
        deliveryOrderForm.set("problemCode", ""); 
        deliveryOrderForm.set("orderStatus", ""); 
        deliveryOrderForm.set("scheduler", ""); 
        deliveryOrderForm.set("orderNote", ""); 
        deliveryOrderForm.set("deliveryFee", ""); 
        deliveryOrderForm.set("pkgCount", "");
       // 将Struts form 设置到request中
       request.setAttribute("deliveryOrderForm", deliveryOrderForm);
       return mapping.findForward("DeliveryOrderCreate.screen");
   }
   /**
    * 显示更新操作.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayDeliveryOrderUpdateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       DeliveryOrder deliveryOrder = deliveryOrderService.getDeliveryOrderById(id);
       if (deliveryOrder == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchDeliveryOrder"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
       DynaValidatorForm deliveryOrderForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("deliveryOrderForm")
               .createActionForm(servlet);
	       	   if(deliveryOrder.getId()!=null){
	 	 	   deliveryOrderForm.set("id",String.valueOf(deliveryOrder.getId()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("id",null);
	 	 	   }
	           deliveryOrderForm.set("orderNumber",deliveryOrder.getOrderNumber());
	           deliveryOrderForm.set("orderType",deliveryOrder.getOrderType());
	           deliveryOrderForm.set("vipCustomFlag",deliveryOrder.getVipCustomFlag());
	           deliveryOrderForm.set("sender",deliveryOrder.getSender());
	           deliveryOrderForm.set("fetchAddr",deliveryOrder.getFetchAddr());
	           deliveryOrderForm.set("senderPhone",deliveryOrder.getSenderPhone());
	           deliveryOrderForm.set("receiver",deliveryOrder.getReceiver());
	           deliveryOrderForm.set("receiverAddr",deliveryOrder.getReceiverAddr());
	           deliveryOrderForm.set("receiverPhone",deliveryOrder.getReceiverPhone());
	           deliveryOrderForm.set("originLocation",deliveryOrder.getOriginLocation());
	           deliveryOrderForm.set("destLocation",deliveryOrder.getDestLocation());
	       	   if(deliveryOrder.getChargeWeight()!=null){
	 	 	   deliveryOrderForm.set("chargeWeight",String.valueOf(deliveryOrder.getChargeWeight()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("chargeWeight",null);
	 	 	   }
	           deliveryOrderForm.set("dimension",deliveryOrder.getDimension());
	           deliveryOrderForm.set("contentsDesc",deliveryOrder.getContentsDesc());
	           deliveryOrderForm.set("payType",deliveryOrder.getPayType());
	           deliveryOrderForm.set("specialOperateNote",deliveryOrder.getSpecialOperateNote());
	       	   if(deliveryOrder.getInsuranceFee()!=null){
	 	 	   deliveryOrderForm.set("insuranceFee",String.valueOf(deliveryOrder.getInsuranceFee()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("insuranceFee",null);
	 	 	   }
	       	   if(deliveryOrder.getAgencyReceiveFee()!=null){
	 	 	   deliveryOrderForm.set("agencyReceiveFee",String.valueOf(deliveryOrder.getAgencyReceiveFee()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("agencyReceiveFee",null);
	 	 	   }
		       DateFormat DATE_FAMAT_ORDERDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
		       if(deliveryOrder.getOrderDate()!=null){
		       deliveryOrderForm.set("orderDate", DATE_FAMAT_ORDERDATE.format(deliveryOrder.getOrderDate()));}
		       else{deliveryOrderForm.set("orderDate",null);}
	           deliveryOrderForm.set("goodsName",deliveryOrder.getGoodsName());
	       	   if(deliveryOrder.getGoodsPrice()!=null){
	 	 	   deliveryOrderForm.set("goodsPrice",String.valueOf(deliveryOrder.getGoodsPrice()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("goodsPrice",null);
	 	 	   }
	           deliveryOrderForm.set("serviceType",deliveryOrder.getServiceType());
	           deliveryOrderForm.set("subStation",deliveryOrder.getSubStation());
	           deliveryOrderForm.set("creater",deliveryOrder.getCreater());
	           deliveryOrderForm.set("fetcher",deliveryOrder.getFetcher());
	           deliveryOrderForm.set("dispatcher",deliveryOrder.getDispatcher());
	           deliveryOrderForm.set("problemCode",deliveryOrder.getProblemCode());
	           deliveryOrderForm.set("orderStatus",deliveryOrder.getOrderStatus());
	           deliveryOrderForm.set("scheduler",deliveryOrder.getScheduler());
	           deliveryOrderForm.set("orderNote",deliveryOrder.getOrderNote());
	       	   if(deliveryOrder.getDeliveryFee()!=null){
	 	 	   deliveryOrderForm.set("deliveryFee",String.valueOf(deliveryOrder.getDeliveryFee()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("deliveryFee",null);
	 	 	   }
	       	   if(deliveryOrder.getPkgCount()!=null){
	 	 	   deliveryOrderForm.set("pkgCount",String.valueOf(deliveryOrder.getPkgCount()));
	 	 	   }else{
	 	 	   deliveryOrderForm.set("pkgCount",null);
	 	 	   }
       // 将Struts form 设置到request中
       request.setAttribute("deliveryOrderForm", deliveryOrderForm);
       // 设置token 防止重复提交
       this.saveToken(request);
       return mapping.findForward("DeliveryOrderUpdate.screen");
   }
	/**
    * 显示预览.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayDeliveryOrderBrowseScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       DeliveryOrder deliveryOrder = deliveryOrderService.getDeliveryOrderById(id);
       if (deliveryOrder == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchDeliveryOrder"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
   	   request.setAttribute("RQ_DELIVERYORDER",deliveryOrder);
       return mapping.findForward("DeliveryOrderBrowse.screen");
   }
   /**
    * @param pDeliveryOrderService the deliveryOrderService to set
    */
   public void setDeliveryOrderService(DeliveryOrderService pDeliveryOrderService) {
       deliveryOrderService = pDeliveryOrderService;
   }
}
